package kr.peep.chickcal233;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

	EditText result;
	String inputNum = "";
	String firstValue, secondValue;
	Double resultValue;
	int operator;
	int i = 0;
	Button btn_add, btn_sub, btn_mul, btn_div;
	Vibrator vibrator;
	Typeface font;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		font = Typeface.createFromAsset(getAssets(), "BradBunR.ttf");

		result = (EditText) findViewById(R.id.resulttv);
		result.setInputType(0);
		result.setTypeface(font);

		findViewById(R.id.btn00).setOnClickListener(this);
		findViewById(R.id.btn01).setOnClickListener(this);
		findViewById(R.id.btn02).setOnClickListener(this);
		findViewById(R.id.btn03).setOnClickListener(this);
		findViewById(R.id.btn04).setOnClickListener(this);
		findViewById(R.id.btn05).setOnClickListener(this);
		findViewById(R.id.btn06).setOnClickListener(this);
		findViewById(R.id.btn07).setOnClickListener(this);
		findViewById(R.id.btn08).setOnClickListener(this);
		findViewById(R.id.btn09).setOnClickListener(this);
		findViewById(R.id.btn_clear).setOnClickListener(this);

		btn_add = new Button(this);
		btn_add.setOnClickListener(this);

		btn_sub = new Button(this);
		btn_sub.setOnClickListener(this);

		btn_mul = new Button(this);
		btn_mul.setOnClickListener(this);

		btn_div = new Button(this);
		btn_div.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn00:
			// Toast.makeText(this, "peep! 0 ", Toast.LENGTH_SHORT).show();
			if (!result.toString().isEmpty() && !inputNum.isEmpty()) {
				result.setText(inputNum += "0");
			} else if (result.toString().equals("")) {
				inputNum = "0";
				result.setText("0");
			}
			vibrator.vibrate(50);
			break;
		case R.id.btn01:
			// Toast.makeText(this, "peep! 1 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "1");
			vibrator.vibrate(50);
			break;
		case R.id.btn02:
			// Toast.makeText(this, "peep! 2 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "2");
			vibrator.vibrate(50);
			break;
		case R.id.btn03:
			// Toast.makeText(this, "peep! 3 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "3");
			vibrator.vibrate(50);
			break;
		case R.id.btn04:
			// Toast.makeText(this, "peep! 4 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "4");
			vibrator.vibrate(50);
			break;
		case R.id.btn05:
			// Toast.makeText(this, "peep! 5 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "5");
			vibrator.vibrate(50);
			break;
		case R.id.btn06:
			// Toast.makeText(this, "peep! 6 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "6");
			vibrator.vibrate(50);
			break;
		case R.id.btn07:
			// Toast.makeText(this, "peep! 7 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "7");
			vibrator.vibrate(50);
			break;
		case R.id.btn08:
			// Toast.makeText(this, "peep! 8 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "8");
			vibrator.vibrate(50);
			break;
		case R.id.btn09:
			// Toast.makeText(this, "peep! 9 ", Toast.LENGTH_SHORT).show();
			result.setText(inputNum += "9");
			vibrator.vibrate(50);
			break;
		case R.id.btn_add:
			operator = 1;
			if (result.toString().isEmpty()) {
				throwErrors();
			} else {
				firstValue = inputNum;
				result.setText(firstValue + "+");
				inputNum = "";
			}
			vibrator.vibrate(50);
			// Toast.makeText(this, "peep! + ", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btn_sub:
			operator = 2;
			if (result.toString().isEmpty()) {
				throwErrors();
			} else {
				// Toast.makeText(this, "peep! - ", Toast.LENGTH_SHORT).show();
				firstValue = inputNum;
				result.setText(firstValue + "-");
				inputNum = "";
			}
			vibrator.vibrate(50);
			break;
		case R.id.btn_mul:
			// Toast.makeText(this, "peep! * ", Toast.LENGTH_SHORT).show();
			operator = 3;
			if (result.toString().isEmpty()) {
				throwErrors();
			} else {
				firstValue = inputNum;
				result.setText(firstValue + "×");
				inputNum = "";
			}
			vibrator.vibrate(50);
			break;
		case R.id.btn_div:
			operator = 4;
			// Toast.makeText(this, "peep! / ", Toast.LENGTH_SHORT).show();
			if (result.toString().isEmpty()) {
				throwErrors();
			} else {
				firstValue = inputNum;
				result.setText(firstValue + "÷");
				inputNum = "";
			}
			vibrator.vibrate(50);
			break;
		case R.id.btn_clear:
			// Toast.makeText(this, "Cleared!", Toast.LENGTH_SHORT).show();
			result.setText("0");
			inputNum = "";
			firstValue = "";
			secondValue = "";
			resultValue = 0.0;
			i = 0;
			vibrator.vibrate(50);
			break;
		case R.id.btn_result:
			vibrator.vibrate(50);
			try {
				if (result.getText().equals("")
						|| result.getText().equals(firstValue)) {
					throwErrors();
				} else {
					secondValue = inputNum;
					switch (operator) {
					case 1:
						resultValue = Double.valueOf(firstValue).doubleValue()
								+ Double.valueOf(secondValue).doubleValue();

						break;
					case 2:
						resultValue = Double.valueOf(firstValue).doubleValue()
								- Double.valueOf(secondValue).doubleValue();
						break;
					case 3:
						resultValue = Double.valueOf(firstValue).doubleValue()
								* Double.valueOf(secondValue).doubleValue();
						break;
					case 4:
						resultValue = Double.valueOf(firstValue).doubleValue()
								/ Double.valueOf(secondValue).doubleValue();

						break;
					}
					if (resultValue.intValue() == resultValue) {
						i = resultValue.intValue();
						result.setText(String.valueOf(i));
						inputNum = String.valueOf(i);
					} else {
						result.setText(String.valueOf(resultValue));
						inputNum = String.valueOf(resultValue);
					}
				}
			} catch (Exception e) {
				throwErrors();
			}
		}
	}

	private void throwErrors() {
		resultValue = 0.0;
		i = 0;
		result.setText("0");
		Toast.makeText(this, "Error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		vibrator.cancel();
	}
}
